package thread;

import java.util.*;

/**
 * 并发安全的集合API
 * ArrayList, LinkedList, HashSet 都不是并发安全的(异步)
 */
public class SyncDemo4 {
    public static void main(String[] args) {
        //List<Integer> c = new ArrayList<>(); //sb=1800~2000
        //List<Integer> c = new LinkedList<>(); //sb=1000~2000
        Set<Integer> c = new HashSet<>(); //sb=2000
        Thread t1 = new Thread(){
          public void run(){
              for(int i =0; i<1000; i++ ){
                  c.add(i);
              }
          }
        };
        Thread t2 = new Thread(){
            public void run(){
                for(int i = 1000; i<2000; i++){
                    c.add(i);
                }
            }
        };
        t1.start();
        t2.start();
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        }
        System.out.println(c.size());//2000
    }
}
